package com.wancaijinfu.widget;

import com.wancaijinfu.actitvity.R;
import com.wancaijinfu.constant.Constant;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class RollViewPager extends ViewPager {

	private int currentItem;
	private final int ROLL_DURATION = 2000;
	private boolean mIsChanged = false;
	private ViewPagerTask viewPagerTask;
	private PagerAdapter adapter;
	
	private LinearLayout indicateLayout;

	public RollViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		if (viewPagerTask == null)
			viewPagerTask = new ViewPagerTask();
	}

	public RollViewPager(Context context) {
		super(context);
		if (viewPagerTask == null)
			viewPagerTask = new ViewPagerTask();
	}

	public void init(boolean isAutoRoll,
			PagerAdapter adapter) {
		this.adapter = adapter;

		initDots();
		
		setOnPageChangeListener(new SimpleOnPageChangeListener());
		setAdapter(adapter);
		
		if (isAutoRoll)
			handler.postDelayed(viewPagerTask, ROLL_DURATION);
	}

	private void initDots() {
		View parent =  (View) getParent();
		indicateLayout = (LinearLayout) parent.findViewById(R.id.index0_indicate);
		
		indicateLayout.removeAllViews();
		for (int i = 0; i < adapter.getCount(); i++) {
			ImageView imageView = new ImageView(getContext());
			
			int size = Constant.getDP_TO_PIX(getContext(), 7);
			int margin = Constant.getDP_TO_PIX(getContext(), 2);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(size, size);
			lp.setMargins(margin, 0, margin, 0);
			imageView.setLayoutParams(lp);
			imageView.setBackgroundResource(R.drawable.circle_back_style);
			indicateLayout.addView(imageView);
		}
		if(indicateLayout.getChildAt(0)!=null)
		indicateLayout.getChildAt(0).setBackgroundResource(R.drawable.circle_select_back_style);
	}

	private OnViewPagerChangeListener onViewPagerChangeListener;

	public void setOnViewPagerChangeListener(
			OnViewPagerChangeListener onViewPagerChangeListener) {
		this.onViewPagerChangeListener = onViewPagerChangeListener;
	}

	public class ViewPagerTask implements Runnable {
		@Override
		public void run() {
			if (adapter.getCount() > 0) {
				long offTime = System.currentTimeMillis() - currentTimeMillis;
				if (offTime > 2200)
					currentItem = (currentItem + 1) % adapter.getCount();
				handler.obtainMessage().sendToTarget();
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			setCurrentItem(currentItem);
			handler.postDelayed(viewPagerTask, ROLL_DURATION);
		}
	};

	private long currentTimeMillis = 0;

	class SimpleOnPageChangeListener implements OnPageChangeListener {
		int oldPosition = 0;

		@Override
		public void onPageSelected(int position) {
			
			currentTimeMillis = System.currentTimeMillis();

			mIsChanged = true;
			currentItem = position;
			
			if (indicateLayout != null) {
				indicateLayout.getChildAt(oldPosition).setBackgroundResource(R.drawable.circle_back_style);
				indicateLayout.getChildAt(position).setBackgroundResource(R.drawable.circle_select_back_style);
			}
			oldPosition = position;
			if (onViewPagerChangeListener != null)
				onViewPagerChangeListener.onPageSelected(position);
		}

		@Override
		public void onPageScrollStateChanged(int pState) {

			if (ViewPager.SCROLL_STATE_IDLE == pState) {
				if (mIsChanged) {
					mIsChanged = false;
					RollViewPager.this.setCurrentItem(currentItem, false);
				}
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
	}

	public interface OnViewPagerChangeListener {
		void onPageSelected(int position);
	}

	public void setCallBallBacksAndMessages(boolean flag) {
		if (!flag)
			handler.removeCallbacksAndMessages(null);
		else {
			if (indicateLayout != null)
				handler.postDelayed(viewPagerTask, ROLL_DURATION);
		}
	}
	
	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}
}
